package com.haima.ziliao.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.haima.common.utils.DateUtils;
import com.haima.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.haima.common.annotation.Log;
import com.haima.common.core.controller.BaseController;
import com.haima.common.core.domain.AjaxResult;
import com.haima.common.enums.BusinessType;
import com.haima.ziliao.domain.PUservip;
import com.haima.ziliao.service.IPUservipService;
import com.haima.common.utils.poi.ExcelUtil;
import com.haima.common.core.page.TableDataInfo;

/**
 * 用户会员Controller
 * 
 * @author hima
 * @date 2024-10-21
 */
@RestController
@RequestMapping("/ziliao/uservip")
public class PUservipController extends BaseController
{
    @Autowired
    private IPUservipService pUservipService;

    /**
     * 查询用户会员列表
     */
    @PreAuthorize("@ss.hasPermi('ziliao:uservip:list')")
    @GetMapping("/list")
    public TableDataInfo list(PUservip pUservip)
    {
        startPage();
        List<PUservip> list = pUservipService.selectPUservipList(pUservip);
        return getDataTable(list);
    }

    @GetMapping("/getmyvip")
    public AjaxResult getmyvip()
    {
        PUservip vip = pUservipService.getOne(new QueryWrapper<PUservip>().eq("uid", SecurityUtils.getUserId()));

        if(vip!=null){
            Date endTime = vip.getEndTime();

            if(endTime.before(DateUtils.getNowDate())){//已过期 更新一下
                vip.setStatus("2");
                pUservipService.updatePUservip(vip);
            }

            return AjaxResult.success(vip);
        }
        return AjaxResult.success(vip);
    }

    /**
     * 导出用户会员列表
     */
    @PreAuthorize("@ss.hasPermi('ziliao:uservip:export')")
    @Log(title = "用户会员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PUservip pUservip)
    {
        List<PUservip> list = pUservipService.selectPUservipList(pUservip);
        ExcelUtil<PUservip> util = new ExcelUtil<PUservip>(PUservip.class);
        util.exportExcel(response, list, "用户会员数据");
    }

    /**
     * 获取用户会员详细信息
     */
    @PreAuthorize("@ss.hasPermi('ziliao:uservip:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pUservipService.selectPUservipById(id));
    }

    /**
     * 新增用户会员
     */
    @PreAuthorize("@ss.hasPermi('ziliao:uservip:add')")
    @Log(title = "用户会员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PUservip pUservip)
    {
        return toAjax(pUservipService.insertPUservip(pUservip));
    }

    /**
     * 修改用户会员
     */
    @PreAuthorize("@ss.hasPermi('ziliao:uservip:edit')")
    @Log(title = "用户会员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PUservip pUservip)
    {
        return toAjax(pUservipService.updatePUservip(pUservip));
    }

    /**
     * 删除用户会员
     */
    @PreAuthorize("@ss.hasPermi('ziliao:uservip:remove')")
    @Log(title = "用户会员", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pUservipService.deletePUservipByIds(ids));
    }
}
